package cn.edu.java.thread.原子类;

import lombok.SneakyThrows;

import java.util.concurrent.atomic.LongAdder;

public class LongAdderDemo {
    @SneakyThrows
    public static void main(String[] args) {
        // 创建LongAdder对象
        LongAdder adder = new LongAdder();

        // 创建并启动多个累加线程
        Thread[] threads = new Thread[50];
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(() -> {
                // 执行累加操作
                for (int j = 0; j < 10000; j++) {
                    adder.increment();
                }
            });
            threads[i].start();
        }

        // 等待所有线程执行完成
        for (Thread thread : threads) {
            thread.join();
        }

        // 输出累加结果
        System.out.println("累加结果: " + adder.sum());
    }
}

